﻿#region All usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using senSimulator.DrawableComponents.Vehicles;
#endregion


namespace senSimulator.DrawableComponents.TrafficLights.Types
{
    class TrafficLightForBusses : TrafficLight
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public TrafficLightForBusses(Game game, Vector2 spawnPosition, Texture2D[] textureNames, Vector2[] textureCenters, int posX, int posY, int rotationInDegrees, string trafficLightID, int distanceToTrafficLight)
            : base(game, spawnPosition, textureNames, textureCenters, posX, posY, rotationInDegrees, trafficLightID, distanceToTrafficLight)
        { }


        /// <summary>
        /// Change the traffic light state
        /// </summary>
        /// <param name="state"></param>
        public override void ChangeTrafficLightState(int state)
        {
            switch (state)
            {
                case 0:     // OFF
                case 7:     // ERROR
                    SwitchOff();
                    break;
                case 2:     // ORANGE;
                case 3:     // RED
                case 4:     // LEFT
                case 5:     // RIGHT
                case 6:     // STRAIGHT FORWARD
                    _trafficLightState = state - 1;
                    _errorAndOffstate = false;
                    break;
                default:    // NOT SUPPORTED
                    Console.WriteLine("Bus traffic light does not support state number: " + state);
                    break;
            }
        }


        /// <summary>
        /// Move a Bus based on the different states
        /// </summary>
        public override void MoveTraffic(Controller controller)
        {
            for (int i = 0; i < _trafficLightQueueLength; i++)
            {
                // Get the Bus and check if it has reached the end
                Bus bus;
                lock (_trafficLightQueue)
                {
                    bus = (Bus)_trafficLightQueue.ElementAt(i);
                }
                if (bus.IsFinished())
                {
                    RemoveTraffic(bus);
                    break;
                }

                // Check if the Bus hits the sensor
                if (bus.GetCurrentWayPoint() == 1 && !bus.SensorTriggered() && bus.GetPosition() != bus.GetFirstWayPoint())
                {
                    bus.SetSensorTriggered(true);
                    controller.SendSensorTriggerToController(this.getName(), 1, bus.GetTo());
                }

                // Check if the vehicle may move
                if (bus.SensorTriggered() || _errorAndOffstate)
                    bus.Move();
                else if (bus.GetCurrentWayPoint() == 0)
                {
                    if (i > 0)
                    {
                        Bus previousBus;
                        lock (_trafficLightQueue)
                        {
                            previousBus = (Bus)_trafficLightQueue.ElementAt(i - 1);
                        }
                        if (previousBus.SensorTriggered())
                            bus.Move();
                        else if ((Math.Abs(bus.GetPosition().X - previousBus.GetPosition().X) + Math.Abs(bus.GetPosition().Y - previousBus.GetPosition().Y) > previousBus.GetLength()))
                        {
                            bus.Move(previousBus.GetPosition(), previousBus.GetLength());
                        }
                        else
                            bus.Stop();
                    }
                    else
                        bus.Move();
                }
                else if (bus.GetDirection() == 'L' && _trafficLightState == 3)
                    bus.Move();
                else if (bus.GetDirection() == 'R' && _trafficLightState == 4)
                    bus.Move();
                else if (bus.GetDirection() == 'S' && _trafficLightState == 5)
                    bus.Move();
                else
                    bus.Stop();
            }
        }
    }
}